package com.example.xcxhouduan.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.xcxhouduan.bean.*;
import com.example.xcxhouduan.dao.envirMapper;
import com.example.xcxhouduan.dao.recomMapper;
import com.example.xcxhouduan.dao.restMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class DataService {
    @Autowired
    private restMapper rM; // 假设restMapper的正确类型是RestMapper
    @Autowired
    private envirMapper eM;
    @Autowired
    private recomMapper recomM;
    public String getEmptyProduct() throws JsonProcessingException, JsonProcessingException {
        // 创建一个可变的产品列表
        List<Product> products = new ArrayList<>();
        List<Restaurants> restList = rM.selectList(null);


        for (Restaurants restaurant : restList) { // 使用增强for循环简化代码
            int restaurant_id = restaurant.getId();
            List<Environmentimages> envirList;
            List<Recommendations> recomList;

            QueryWrapper<Environmentimages> queryEnvir = new QueryWrapper<>();
            queryEnvir.eq("restaurant_id", restaurant_id); // 假设 restaurant_id 是 Environmentimages 表的一个字段

            QueryWrapper<Recommendations> queryRecom = new QueryWrapper<>();
            queryRecom.eq("restaurant_id", restaurant_id); // 假设 restaurant_id 是 Recommendations 表的一个字段

            envirList = eM.selectList(queryEnvir); // 假设 eM 已经是指向 EnvironmentimagesMapper 的接口

            recomList = recomM.selectList(queryRecom); // 假设 recomM 已经是指向 RecommendationsMapper 的接口


            Product product = new Product();
            product.setId(restaurant.getId());
            product.setTitle(restaurant.getTitle());
            product.setName(restaurant.getName());
            product.setImage1(restaurant.getImage1());
            product.setTelephone(restaurant.getTelephone());
            product.setAddress(restaurant.getAddress());
            product.setBusiness(restaurant.getBusiness());
            product.setIsClick(restaurant.getIsclick());
            product.setBuys(restaurant.getBuys());
            product.setPays(restaurant.getPays());
            product.setLongitude(restaurant.getLongitude());
            product.setLatitude(restaurant.getLatitude());
            product.setTuijian(recomList);
            product.setHuanjing(envirList);

            products.add(product);
        }

        // 创建DataResponse对象
        DataResponse dataResponse = new DataResponse(products);

        // 返回JSON字符串
        return dataResponse.toJsonString();
    }
}
